package com.sansi.bleFloodlight.ble;

public class ScanRecordUtil {
    static final char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * 解析广播包
     */
    private static String[] scanResult = new String[3];

    public static String[] parseAdvertising(byte[] advertising) {
        String advertisingStr = ScanRecordUtil.bytesToHex(advertising);

        String companyName = advertisingStr.substring(22, 32);
        String deviceType = advertisingStr.substring(32, 36);
        String deviceMac = advertisingStr.substring(62, 74);

        scanResult[0] = companyName;
        scanResult[1] = deviceType;
        scanResult[2] = deviceMac;

        return scanResult;
    }
}
